package game;
/*
 * This class handels all the rules in the game.
 */

public class FourInRowRules {

    private int[][] grid;
    final int ROWS = 6, COLUMNS = 7;

    protected FourInRowRules(int[][] grid) {
        this.grid = grid;
    }

    private int findTopDisc(int coloumn) {
        for (int row = 0; row < ROWS; row++) {
            if (grid[row][coloumn] != 0) {
                return row;
            }
        }
        return 0;
    }

    public boolean gameWon(int coloumn) {
        int row = findTopDisc(coloumn);
        if (gameWonHorizontal(row)
                || gameWonVertical(coloumn)
                || gameWonDiagonalDown(coloumn, row)
                || gameWonDiagonalUp(coloumn, row)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean gameWonHorizontal(int row) {
        for (int i = 0; i < COLUMNS - 3; i++) {
            if (grid[row][i] != 0
                    && grid[row][i + 1] == grid[row][i]
                    && grid[row][i + 2] == grid[row][i]
                    && grid[row][i + 3] == grid[row][i]) {
                return true;
            }
        }
        return false;
    }

    private boolean gameWonVertical(int coloumn) {
        for (int i = 0; i < ROWS - 3; i++) {
            if (grid[i][coloumn] != 0
                    && grid[i + 1][coloumn] == grid[i][coloumn]
                    && grid[i + 2][coloumn] == grid[i][coloumn]
                    && grid[i + 3][coloumn] == grid[i][coloumn]) {
                return true;
            }
        }
        return false;
    }

    private boolean gameWonDiagonalDown(int coloumn, int row) {
        int startRow = row;
        int startColoumn = coloumn;

        while (startColoumn > 0 && startRow > 0) {
            startColoumn--;
            startRow--;
        }
        try {
            for (int i = startRow, j = startColoumn; i < COLUMNS && j < ROWS; i++, j++) {
                if (grid[i][j] != 0
                        && grid[i + 1][j + 1] == grid[i][j]
                        && grid[i + 2][j + 2] == grid[i][j]
                        && grid[i + 3][j + 3] == grid[i][j]) {
                    return true;
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            return false;
        }
        return false;

    }

    private boolean gameWonDiagonalUp(int coloumn, int row) {
        int startRow = row;
        int startColoumn = coloumn;

        while (startColoumn > 0 && startRow < ROWS - 1) {
            startColoumn--;
            startRow++;
        }
        try {
            for (int i = startRow, j = startColoumn; i < COLUMNS && j < ROWS; i--, j++) {
                if (grid[i][j] != 0
                        && grid[i - 1][j + 1] == grid[i][j]
                        && grid[i - 2][j + 2] == grid[i][j]
                        && grid[i - 3][j + 3] == grid[i][j]) {
                    return true;
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            return false;
        }
        return false;
    }

    public boolean boardIsFull() {
        for (int column = 0; column < COLUMNS; column++) {
            if (grid[0][column] == 0) {
                return false;
            }
        }
        return true;
    }
}
